<?php
/**
 *
 */
namespace app\common\query;
use app\common\model\MallCategory as MallCategoryModel;
use app\common\cache\CacheData;

class MallCategory
{

    public static function getCategoryDetail($id='',$fields=''){
         $model = new MallCategoryModel;
         return $model->where(['id'=>$id])->field($fields)->find();
    }

    /**
     * 查询多条数据
     * @return [type] [description]
     */
    public static function getDatas($pid=0){
        return self::getAllCates($pid);
    }


    /**
     * [deleteOneData 删除分类]
     * @param  string $data [description]
     * @return [type]       [description]
     */
    public function deleteOneData($id=''){
        $model = new MallCategoryModel;
        $data = $model->where(['id'=>$id])->whereOr(['pid'=>$id])->select()->toArray();
        foreach ($data as $v) {
            $model->where(['id'=>$v['id']])->delete();
        }
        return ['data'=>'操作成功'];
    }

     /**
     * 新增分类
     * @param [type] $data [description]
     */
    public function addOneData($data=[]){

        $model = new MallCategoryModel;
        return $model->save($data);
    }

    /**
     * 更新会员资料
     * @return [type] [description]
     */
    public function saveOneData($data=[],$id=''){
        $model = new MallCategoryModel;
        return $model->save($data,['id' => $id]);
    }

    public static function getAllCates($pid=0){
        $model = new MallCategoryModel;
        $cates=$model->where(['pid'=>$pid])->select();
        $arr=array();
        foreach ($cates as $k => $v) {
            $v['child']=self::getAllCates($v['id']);
            $v['tops'] = 1;
            if(self::getAllCates($v['id']) == null)$v['tops'] = 0;
            $arr[]=$v;
        }
        return $arr;
    }
}